<?php
require_once(dirname(__FILE__)."/../common/constants.php");
require_once(DB_SESSION_DIR_PATH."/db_operator.php");
require_once(DB_SESSION_DIR_PATH."/db_obj_defs.php");
/*
 * Created on
 * Date: 2007-12-4
 * Author: William Yang
 */

class TeamMemberManager extends TeamMember {
	public function register() {
		$stat = parent::register(CONFIGURATION_DIR_PATH."/create_team_member.xml",
								FALSE);
		return $stat;
	}
	
	/*
	 * Delete a member specified by @$member_uid from the team specified by $this->uid;
	 * 
	 * Return value: TRUE if operation fails, otherwise FALSE.
	 */
	public static function delete_member($team_uid, $member_uid) {
		$operator = new DatabaseOperator();
		$operator->config(CONFIGURATION_DIR_PATH."/team_member_table.xml");
		
		$c1 = new SqlCondition("teamid", SqlCondition::EQUAL, $team_uid);
		$c2 = new SqlCondition("memberid", SqlCondition::EQUAL, $member_uid);
		$cList = new SqlCondList();
		$cList->add_condition($c1);
		$cList->add_relation(SqlCondition::_AND);
		$cList->add_condition($c2);
		
		$db_stat = $operator->delete($cList);
		return ($db_stat == DbError::DB_OK);
	}
	
	public static function query_team_by_user_uids($usr_uids) {
		$operator = new DatabaseOperator();
		$operator->config(CONFIGURATION_DIR_PATH."/team_member_table.xml");
		
		$cList = new SqlCondList();
		if (gettype($usr_uids) === gettype(array())) {
			$cnt = count($usr_uids);
			for ($i = 0; $i < $cnt - 1; ++$i) {
				$cList->add_condition(new SqlCondition("memberid", SqlCondition::EQUAL, $usr_uids[$i]));
				$cList->add_relation(SqlCondition::_OR);
			}
			$cList->add_condition(new SqlCondition("memberid", SqlCondition::EQUAL, $usr_uids[$cnt - 1]));
		} else
			$cList->add_condition(new SqlCondition("memberid", SqlCondition::EQUAL, $usr_uids));
		
		$ret_array = $operator->select($cList);
		
		$ret_objs = array();
		$tmobj = new TeamMemberManager();
		foreach ($ret_array as $ret) {
			$ret_objs[] = $tmobj->array2copy($ret);
		}
		return $ret_objs;
	}
	
	public static function query_by_team_uids($team_uids) {
		$operator = new DatabaseOperator();
		$operator->config(CONFIGURATION_DIR_PATH."/team_member_table.xml");
		
		$cList = new SqlCondList();
		if (gettype($team_uids) === gettype(array())) {
			$cnt = count($team_uids);
			for ($i = 0; $i < $cnt - 1; ++$i) {
				$cList->add_condition(new SqlCondition("teamid", SqlCondition::EQUAL, $team_uids[$i]));
				$cList->add_relation(SqlCondition::_OR);
			}
			$cList->add_condition(new SqlCondition("teamid", SqlCondition::EQUAL, $team_uids[$cnt - 1]));
		} else
			$cList->add_condition(new SqlCondition("teamid", SqlCondition::EQUAL, $team_uids));
		
		$ret_array = $operator->select($cList);
		
		$ret_objs = array();
		$tmobj = new TeamMemberManager();
		foreach ($ret_array as $ret) {
			$ret_objs[] = $tmobj->array2copy($ret);
		}
		return $ret_objs;
	}
}
?>